﻿using MediatR;
using Newtonsoft.Json;
using Warehouse.API.Applcation.Command.EnterStoragesCommand;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using WMSpublic;

namespace Warehouse.API.Applcation.CommandHandlers.EnterStoragesCommandHandler
{
    /// <summary>
    /// 入库信息删除
    /// </summary>
    public class EnterStoragesDeleteCommandHandles : IRequestHandler<EnterStoragesDeleteCommand, int>
    {
        private readonly IEnterStoragesRepsitory _enterStoragesRepository;
        public EnterStoragesDeleteCommandHandles(IEnterStoragesRepsitory enterStoragesRepository)
        {
            _enterStoragesRepository = enterStoragesRepository;
        }

        public async Task<int> Handle(EnterStoragesDeleteCommand request, CancellationToken cancellationToken)
        {
            foreach (var item in request.ids)
            {
                var list = await _enterStoragesRepository.GetModelAsync(item);
                list.IsDel = true;
                list.DelTime = DateTime.Now;
                list.DelName=request.DelName;
                await _enterStoragesRepository.UpdateAsync(list);
            }
            MessageCreateDto a = new MessageCreateDto();
            a.AddName = "张三";
            a.AddTime = DateTime.Now;
            a.Description = "入库删除";
            a.Title = "入库删除";
            a.CurrenTime = DateTime.Now;
            a.MessageType = "删除";
            a.MessageState = false;
            a.Office_Id = "1";
            await MessageAdds(a);
            return 1;
        }

        public async Task<int> MessageAdds(MessageCreateDto a)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/xxx/api/MessageCreate/CreateMessage";
            //将a转换为json字符串
            string jsonStr = JsonConvert.SerializeObject(a);
            string postRep = await client.PostAsync(url, jsonStr);
            return 1;
        }
    }
}
